﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using ExcelReader.ModelHelper;
using System.IO;
using System.Linq;
using System;

namespace ExcelReaderTest
{
    [TestClass]
    public class UnitTest1
    {
        private const string FilePath = @"C:\Sandbox\ExcelReader\xlsx\Book1.xlsx";
        private const string FilePath2 = @"C:\Sandbox\ExcelReader\xlsx\Book2.xlsx";

        //[TestMethod]
        public void TestMethod1()
        {
            //initialize parameter to pass
            var xlsxFile = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
            var model1 = new Model1();
            var reader = new ExcelReader.Reader(xlsxFile);
            var rows = reader.GetWorksheet(model1, "Sheet1", 2, 0);
            var records = rows.Cast<Model1>();

            var filtered = records.Where(p => p.LastName.Contains("Kamal"));

            Assert.IsNotNull(filtered);
        }

        [TestMethod]
        public void TestMethod2()
        {
            //initialize parameter to pass
            var xlsxFile = new FileStream(FilePath2, FileMode.Open, FileAccess.Read);
            var model2 = new Model2();
            var reader = new ExcelReader.Reader(xlsxFile);
            var rows = reader.GetWorksheet(model2, "Sheet1", 2, 0);
            reader.Dispose();
            var records = rows.Cast<Model2>();

            var filtered = records.Where(p => p.Age >= 30);
            //var filtered = records.Where(p => p.LastName.Contains("Kamal"));

            Assert.IsNotNull(filtered);

        }
    }

    class Model1
    {
        [ColumnMapper("A")]
        public string FirstName { get; set; }
        [ColumnMapper("B")]
        public string LastName { get; set; }
        [ColumnMapper("C")]
        public string Age { get; set; }
    }

    class Model2
    {
        [ColumnMapper("A")]
        public string FirstName { get; set; }
        [ColumnMapper("B")]
        public string LastName { get; set; }
        [ColumnMapper("C")]
        public int Age { get; set; }
        [ColumnMapper("D")]
        public DateTime DOB { get; set; }
    }
}
